package com.nand.blockdemo.ui.feedback;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.HttpParams;
import com.nand.blockdemo.R;
import com.nand.blockdemo.adapter.FeekEditAdapter;
import com.nand.blockdemo.bean.FeekItemBean;
import com.nand.blockdemo.constant.URL;
import com.nand.blockdemo.ui.dialog.PhotoDialog;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.ObjectUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.function.utils.dateutils.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FeekDetailsActivity extends BaseActivity {
    private int userId;
    private String oid;
    private int isHandle;
    private LinearLayout mReplyLayout;
    private LinearLayout mTimeLayout;
    private LinearLayout mPhotoLayout;
    private Button mButton, mButton1;
    private TextView text, text0, text1, text2, text3, text4, text6, text7, text8;
    private EditText mEditReply;
    private RecyclerView mRecyclerView;
    private FeekEditAdapter mAdapter;
    private ArrayList<String> list = new ArrayList<>();

    @Override
    protected boolean isActionBar() {
        return true;
    }


    @Override
    protected int initLayout() {
        return R.layout.activity_feek_details;
    }

    @Override
    protected void initView() {
        mActionBar().setCenterText("反馈详情");

        mReplyLayout = findViewById(R.id.reply_layout);
        mTimeLayout = findViewById(R.id.time_layout);
        mPhotoLayout = findViewById(R.id.photo_layout);

        mButton = findViewById(R.id.button);
        mButton1 = findViewById(R.id.button1);

        text = findViewById(R.id.textView);
        text0 = findViewById(R.id.textView0);
        text1 = findViewById(R.id.textView1);
        text2 = findViewById(R.id.textView2);
        text3 = findViewById(R.id.textView3);
        text4 = findViewById(R.id.textView4);
//        text5 = findViewById(R.id.textView5);
        text6 = findViewById(R.id.textView6);
        text7 = findViewById(R.id.textView7);
        text8 = findViewById(R.id.textView8);

        mEditReply = findViewById(R.id.et_reply);

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter = new FeekEditAdapter(list);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ArrayList<String> data = (ArrayList<String>) adapter.getData();
                Bundle bundle = new Bundle();
                bundle.putInt("currentPostion", position);
                bundle.putStringArrayList("imageData", data);
                PhotoDialog photoDialog = new PhotoDialog();
                photoDialog.setArguments(bundle);
                photoDialog.show(getSupportFragmentManager(), "");
            }
        });
    }

    @Override
    protected void initData() {
        userId = SpUtils.readInt(this, "userId", 0);
        oid = getIntent().getStringExtra("oid");
        isHandle = getIntent().getIntExtra("isHandle", 0);
        getHttpData();
    }

    private void getHttpData() {
        HttpParams paramsPost = new HttpParams();
        paramsPost.put("oid", oid);
        OkGoUtils.getInstance().post(this, URL.FEEDBACK_DETAILS_URL, paramsPost, 0, false, mCallback);
    }

    private MyCallback mCallback = new MyCallback() {
        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                switch (id) {
                    case 0:
                        String data = response.getDataInfo().toString();
                        FeekItemBean item = JSON.parseObject(data, FeekItemBean.class);
                        setViewData(item);
                        break;
                    case 1:
                        showToast("提交成功");
                        finish();
                        break;
                    case 2:
                        showToast("确认完成");
                        finish();
                        break;
                }
            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {

        }
    };

    private void setViewData(FeekItemBean data) {
        int state = data.getState();
        switch (state) {
            case 3:
            case 2:
                Log.e(TAG, "userId：" + userId + "----getFMmUserOid：" + data.getFMmUserOid());
                if (userId == data.getFMmUserOid() && state == 2) {
                    mButton1.setVisibility(View.VISIBLE);
                }
                mEditReply.setEnabled(false);
                mTimeLayout.setClickable(false);
                mEditReply.setText(data.getFeedback());
                text8.setText(data.getResolutionTime().substring(0, 10));
            case 1:
                if (isHandle == 1) {
                    mReplyLayout.setVisibility(View.VISIBLE);
                    mTimeLayout.setVisibility(View.VISIBLE);
                    if (state == 1) {
                        mButton.setVisibility(View.VISIBLE);
                    }
                }
                text.setText(data.getFeedbackoid());
                text0.setText(data.getTitle());
                text1.setText(data.getFeedbackType());
                text2.setText(data.getModuleTitle() + "/" + data.getPageTitle());
                text3.setText(data.getType());
                text4.setText(data.getDescription());
//                text5.setText(data.getDepartName() + "/" + data.getUserName1());
                text6.setText(data.getUsername());
                text7.setText(data.getCreatedtime() + "/" + data.getUsername());

                List<String> list = data.getAttachment1();
                if (ObjectUtils.notEmpty(list)) {
                    mPhotoLayout.setVisibility(View.VISIBLE);
                    // TODO: 2020/3/13 处理照片数据
                    mAdapter.setNewData(list);
                }
                break;
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.time_layout:
                solveTime();
                break;
            case R.id.button:
                submit();
                break;
            case R.id.button1:
                confirm();
                break;
        }
    }

    private void solveTime() {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(FeekDetailsActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String s = DateUtils.formatDate(date, "yyyy-MM-dd");
                text8.setText(s);
            }
        }).setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(14)//确定和取消文字大小
                .setSubmitColor(Color.GRAY)//确定按钮文字颜色
                .setCancelColor(Color.GRAY)//取消按钮文字颜色
                .setContentTextSize(18)//滚轮文字大小
                .isCyclic(true)
                .build();
        pvTime.show();
    }

    private void submit() {
        String str = mEditReply.getText().toString().trim();
        String time = text7.getText().toString();
        if (ObjectUtils.isEmpty(str)) {
            showToast("回复内容不能为空");
            return;
        }
        if ("解决时间".equals(time)) {
            showToast("请选择解决时间");
            return;
        }
        HttpParams params = new HttpParams();
        params.put("oid", oid);
        params.put("userId", userId);
        params.put("type", "3");
        params.put("resolutiontime", time);
        params.put("feedback", str);
        OkGoUtils.getInstance().post(this, URL.FEEDBACK_UPDATE_STATE, params, 1, true, mCallback);
    }

    private void confirm() {
        HttpParams params = new HttpParams();
        params.put("oid", oid);
        params.put("userId", userId);
        params.put("type", "1");
        OkGoUtils.getInstance().post(this, URL.FEEDBACK_UPDATE_STATE, params, 2, true, mCallback);
    }

}
